package com.sandmens.droidvdr;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;

public class settingNetwork2 extends Activity{
	
  	public static final String PREFS_NAME = "droidVDRsettings";
	private CheckBox chk_Network2 = null;
	private EditText URL_mvdr2 = null; 
	private EditText URL_stream2 = null;
	private CheckBox chk_Passwd2 = null;  
	private EditText Passwd_mvdr2 = null; 
	private EditText User_mvdr2 = null;

	
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting_network2);
    	URL_mvdr2 = (EditText) findViewById(R.id.Input_mvdr2);
    	URL_stream2 = (EditText) findViewById(R.id.Input_stream2);
    	chk_Passwd2 = (CheckBox)  findViewById(R.id.Check_UsePassw2);
    	User_mvdr2 = (EditText) findViewById(R.id.Input_User2);
    	Passwd_mvdr2 = (EditText) findViewById(R.id.Input_Password2);
        ReadSettings();
        if ( !chk_Passwd2.isChecked() )
        {
        	User_mvdr2.setEnabled(false);
        	Passwd_mvdr2.setEnabled(false);
        }
        if ( !chk_Passwd2.isChecked() )
        {
        	User_mvdr2.setEnabled(false);
        	Passwd_mvdr2.setEnabled(false);
        }
        chk_Passwd2.setOnClickListener(new OnClickListener()
        {
			@Override
			public void onClick(View v) 
			{
		        if ( !chk_Passwd2.isChecked() )
		        {
		        	User_mvdr2.setEnabled(false);
		        	Passwd_mvdr2.setEnabled(false);
		        }
		        else
		        {
		        	User_mvdr2.setEnabled(true);
		        	Passwd_mvdr2.setEnabled(true);
		        }
			}
        });
    }
    
    @Override
    protected void onStop(){
    	super.onStop();
    }

    @Override
    protected void onStart()
    {
    	super.onStart();
    }
    
    @Override
    protected void onRestart()
    {
    	super.onRestart();
    }

	@Override
    protected void onResume()
    {
    	super.onResume();
    }
    
    @Override
    protected void onPause()
    {
    	super.onPause();
    }
    
    @Override
    protected void onDestroy()
    {
    	super.onDestroy();
    } 

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if ( keyCode == KeyEvent.KEYCODE_BACK)
		{
	    	save_Setting();
        	setResult(RESULT_OK);
			finish();
		}			
		return super.onKeyDown(keyCode, event);
	}

    private void save_Setting()
    {
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("network2_url_mvdr", URL_mvdr2.getText().toString());
        editor.putString("network2_url_stream", URL_stream2.getText().toString());

        editor.putBoolean("network2_use_passwd", chk_Passwd2.isChecked());
        editor.putString("network2_user", User_mvdr2.getText().toString());
        editor.putString("network2_passwd", Passwd_mvdr2.getText().toString());
        editor.commit();
    }

  	private void ReadSettings()
  	{
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    	URL_mvdr2.setText(settings.getString("network2_url_mvdr", "http://server-ip:8080/cgi-bin/mvdr.pl"));
    	URL_stream2.setText(settings.getString("network2_url_stream", "rtsp://vlc-rtsp-ip:1235/test.sdp"));
    	
    	chk_Passwd2.setChecked(settings.getBoolean("network2_use_passwd", false));
    	User_mvdr2.setText(settings.getString("network2_user", "user"));
    	Passwd_mvdr2.setText(settings.getString("network2_passwd", "passwd"));
  	}

}
